package clue

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/errcode"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/clue"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/clue/report"
	"git.myscrm.cn/golang/common/ykerrcode"
	"git.myscrm.cn/golang/stark/v4"
)

type Report struct {
	ReportService report.OpptyReportServiceIface
}

func NewReport(reportService report.OpptyReportServiceIface) pb.ReportServiceServer {
	return &Report{
		ReportService: reportService,
	}
}

func (r *Report) TestTenantDbEnable(ctx context.Context, request *pb.TestTenantDbEnableRequest) (*pb.TestTenantDbEnableResponse, error) {
	res, err := r.ReportService.TestTenantDbEnable(ctx, request)
	return res, err
}

// GetReportList 获取我的报备列表
func (r *Report) GetReportList(ctx context.Context, request *pb.GetReportListRequest) (*pb.GetReportListResponse, error) {
	// 判断用户数据是否为空
	var errMsg string
	if request.GetResponsiblePersonGuid() == "" {
		errMsg = "user_id参数缺失"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}

	if request.GetPage() < 1 {
		request.Page = 1
	}
	if request.GetPageSize() < 1 {
		request.PageSize = 10
	}

	//var resp = pb.GetUserInfoResponse{}
	res, err := r.ReportService.GetReportList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "获取我的报备列表失败：", err)
		return nil, err
	}
	return res, nil
}

func (r *Report) GetRegionReportList(ctx context.Context, request *pb.GetReportListRequest) (*pb.GetReportListResponse, error) {
	// 判断用户数据是否为空

	if request.GetPage() < 1 {
		request.Page = 1
	}
	if request.GetPageSize() < 1 {
		request.PageSize = 10
	}

	res, err := r.ReportService.GetRegionReportList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "获取区域的报备列表失败：", err)
		return nil, err
	}
	return res, nil
}

// SaveFollowRecord 保存跟进记录
func (r *Report) SaveFollowRecord(ctx context.Context, request *pb.SaveFollowRecordRequest) (*pb.SaveFollowRecordResponse, error) {
	var errMsg string

	if request.GetData().ReportId == "" {
		errMsg = "report_id参数缺失"
	} else if request.GetData().GetFollowType() == 0 {
		errMsg = "请选择跟进类型"
	} else if request.GetData().GetFollowStatus() == 0 {
		errMsg = "请选择跟进状态"
	} else if followType := request.GetData().GetFollowType(); followType < 1 || followType > 4 {
		errMsg = "跟进类型不合法"
	} else if followStatus := request.GetData().GetFollowStatus(); followStatus < 1 || followStatus > 3 {
		errMsg = "跟进状态不合法"
	} else if images := request.GetData().GetImages(); images != nil && len(images) > 5 {
		errMsg = "项目图片最多上传5张"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}

	resp, err := r.ReportService.SaveFollowRecord(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "SaveFollowRecord err: %s", err.Error())
		return nil, err
	}
	return resp, nil
}

// GetFollowRecordList 获取跟进列表
func (r *Report) GetFollowRecordList(ctx context.Context, request *pb.GetFollowRecordListRequest) (*pb.GetFollowRecordListResponse, error) {
	var errMsg string
	if request.GetReportId() == "" {
		errMsg = "report_id不能为空"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}

	if request.GetPage() < 1 {
		request.Page = 1
	}
	if request.GetPageSize() < 1 {
		request.PageSize = 10
	}

	resp, err := r.ReportService.GetFollowRecordList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetFollowRecordList err: %s", err.Error())
		return nil, err
	}
	return resp, nil
}

func (r *Report) SaveReport(ctx context.Context, request *pb.SaveReportRequest) (*pb.SaveReportResponse, error) {
	err := r.ReportService.CheckSaveReportParams(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "SaveReport 保存报备信息参数校验不通过，错误信息：%v", err)
		return nil, err
	}

	flag, reportId, err := r.ReportService.SaveReport(ctx, request)
	if err != nil {
		if rpc_client.GetGrpcError(err).GetCode() != errcode.ReportIsExistError {
			stark.Logger.Errorf(ctx, "SaveReport 保存报备信息失败，错误信息：%s", err.Error())
		}
		return nil, err
	}
	return &pb.SaveReportResponse{IsSuccess: flag, ReportId: reportId}, nil
}

// GetReport 获取报备详情
func (r *Report) GetReport(ctx context.Context, request *pb.GetReportRequest) (*pb.GetReportResponse, error) {
	if request.GetReportId() == "" {
		return nil, errors.New("report_id不能为空")
	}

	resp, err := r.ReportService.GetReport(ctx, request.GetReportId())
	if err != nil {
		stark.Logger.Errorf(ctx, "GetReport err: %s", err.Error())
		return nil, err
	}
	return resp, nil
}

func (r *Report) GetReportSourceInfo(ctx context.Context, request *pb.GetReportSourceInfoRequest) (*pb.GetReportSourceInfoResponse, error) {
	//if request.ReportSourceType == "" {
	//	return nil, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "sourct_type不能为空")
	//}
	if request.ReportSourceType != "" && request.ReportSourceId == "" {
		return nil, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "sourct_id不能为空")
	}

	resp, err := r.ReportService.GetReportSourceInfo(ctx, request.ReportSourceType, request.ReportSourceId)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetReportSourceInfo err: %v", err)
		return nil, err
	}
	return resp, nil
}

func (r *Report) GetTeamLeader(ctx context.Context, request *pb.GetTeamLeaderRequest) (*pb.GetTeamLeaderResponse, error) {
	if request.ReportUid == "" {
		return nil, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "report_uid不能为空")
	}
	resp, err := r.ReportService.GetTeamLeader(ctx, request.ReportUid)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetTeamLeader err: %v", err)
		return nil, err
	}

	return resp, nil
}

// MyReportStat 我的报备统计
func (r *Report) MyReportStat(ctx context.Context, request *pb.MyReportStatRequest) (*pb.MyReportStatResponse, error) {
	// 判断用户数据是否为空
	var errMsg string
	if request.GetUserId() == "" {
		//errMsg = "user_id参数缺失"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}

	res, err := r.ReportService.MyReportStat(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "获取我的报备统计失败：", err)
		return nil, err
	}
	return res, nil
}

func (r *Report) GetUserList(ctx context.Context, request *pb.GetUserListRequest) (*pb.GetUserListResponse, error) {
	res, err := r.ReportService.GetUserList(ctx, request)
	if err != nil {
		return nil, err
	}
	return res, nil
}

// 发送分享领取通知
func (r *Report) SendShareReceiveMsg(ctx context.Context, req *pb.SendShareReceiveMsgRequest) (*pb.SendShareReceiveMsgResponse, error) {
	var (
		errMsg string
		resp   = pb.SendShareReceiveMsgResponse{Success: true}
	)
	if req.GetProjectTitle() == "" {
		errMsg = "项目名称不能为空"
	} else if req.GetShareUserTenantId() == "" {
		errMsg = "分享者用户ID不能为空"
	}
	//else if req.GetReceiveMobile() == "" {
	//	errMsg = "领取人手机号不能为空"
	//}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}

	if err := r.ReportService.SendShareReceiveMsg(ctx, req); err != nil {
		resp = pb.SendShareReceiveMsgResponse{Success: false, ErrMsg: err.Error()}
		return &resp, nil
	}
	return &resp, nil
}
